<?php  

    require_once  ("Includes/connectDB.php");

    // convertes hundreths of secs to human readable format
    function hundrethSecsToString ($hsecs)
    {
        $seconds = intval($hsecs/100);
        $hsecs = $hsecs % 100;

        if ($seconds >= 60)
        {
            $minute = intval($seconds/60);
            $seconds = $seconds%60;
        }

        if (isset($minute))
            return "$minute:$seconds.$hsecs";
        else
            return "$seconds.$hsecs";
    }

    // prints out scores by meet
    function printScoresByMeet($meetid, $databaseConnection)
    {
        printScores('m', $meetid, $databaseConnection);
    }

    // prints out scores by swimmer
    function printScoresBySwimmer($swimid, $databaseConnection)
    {
        printScores('s', $swimid, $databaseConnection);
    }

    function printScores($filtertype, $filterid, $databaseConnection) {

        //mysqli_report(MYSQLI_REPORT_ALL);

        //echo $filterid;
        
        if (!isset($filtertype) || !isset($filterid))
            die("Error: filter not set for display scores.");

        // prepare statement by filter
        if ($filtertype == 'm')
           $query = "SELECT events.id, events.name, swimmers.nickname, scores.place, scores.seed_time, ".
                            "scores.final_time, swimmers.id, teams.name, teams.id, scores.relay_team ".
                     "FROM swimmers, events, scores, teams ".
                     "WHERE swimmers.id = scores.swim_id AND events.event_id = scores.event_id AND swimmers.team_id = teams.id ".
                     "AND scores.meet_id = ? ".
                     "ORDER BY events.event_id ASC, sort_pos ASC";
        else if($filtertype == 's')
            $query = "SELECT events.id, events.name, ".
                     "swimmers.nickname, scores.place, scores.seed_time, scores.final_time, meets.date, ".
                     "swimmers.id, meets.id, meets.name ". 
                     "FROM swimmers, events, scores, meets ".
                     "WHERE swimmers.id = scores.swim_id AND events.event_id = scores.event_id AND scores.meet_id = meets.id ".
                     "AND swimmers.id = ? ".
                     "ORDER BY events.event_id ASC, meets.date DESC";
        else
            die("Error: invalid filters type.");

            //var_dump($query);

        // execute statement
        $statement = $databaseConnection->prepare($query);
        if ($statement == FALSE)
            die ("Data query failed: statement not prepared". $query);
        $statement->bind_param('i', $filterid);
        $statement->execute();
        if($statement->error)
            die("Database query failed: " . $statement->error);

        $statement->store_result();

        if ($statement->num_rows == 0) {
            die ("Error: No results found");
        }
?>

<table width="100%" class="tableClass">
<tr><td>Event</td>
    <td>Name</td>
    <td>Place</td>
    <td>Seed</td>
    <td>Final</td>
    <td>!</td>

<?php
    
    if ($filtertype == 'm')
        echo '<td>Team</td></tr>';
    else if ($filtertype == 's')
        echo '<td>Meet</td></tr>';

    // bind results for fetch
    if ($filtertype == 'm')
        $statement->bind_result($eventid, $eventname, $nickname, $place, $seed, $final, 
                                $swimid, $teamname, $teamid, $relay_team);
    else if ($filtertype == 's')
        $statement->bind_result($eventid, $eventname, $nickname, $place, $seed, $final, 
                                $meetdate, $swimid, $meetid, $meetname);

    $lastevent = -1;
    // print out results, one by one
    while($statement->fetch())
    {
        // if last event id doesn't match new event id, place event header
        if($eventid != $lastevent)
        {
            echo '<tr class="headrow"><td colspan="6">Event '.$eventid.' '.$eventname.'</td></tr>';
            $lastevent = $eventid;
        }

        // seed and final time conversion
        //$seed = hundrethSecsToString ($seed);
        //$final = hundrethSecsToString ($final);
        if($final < $seed)
            $improve = '!';
        else 
            $improve = '&nbsp;';

        echo "<tr class=\"newmeet\"><td></td>"; 
        if (isset($teamid) && $teamid == 1)
            echo "<td><a href=\"swimmer.php?swimid=$swimid\">$nickname</a></td>";
        else
            echo "<td>$nickname</td>";
        echo "<td align=\"right\"><b>$place</b></td>";
        echo "<td align=\"right\">$seed</td>";
        echo "<td align=\"right\"><b>$final</b></td>";
        echo "<td>$improve</td>";

        if (isset($relay_team))
            $relay_team = "'".$relay_team."'";

        // if sorting by meet, then display team name column
        // if sorting by swimmer, then display meet column
        if ($filtertype == 'm')
            echo "<td>$teamname $relay_team</td></tr>";
        else if ($filtertype == 's')
        {
            $meetdate = new DateTime($meetdate);
            $meetdate = $meetdate->format('m/d/Y');
            echo "<td><a href=\"meet.php?meetid=$meetid\">$meetname - $meetdate</a></td></tr>";
        }
    }

?>
</table>

<?php
}
?>